package cn.js.tech.terminal

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import cn.js.tech.terminal.ui.theme.TerminalTheme
import com.alipay.android.phone.scancode.export.ScanRequest
import com.alipay.android.phone.scancode.export.adapter.MPScan
import com.alipay.android.phone.scancode.export.adapter.MPScanCallbackAdapter
import com.alipay.android.phone.scancode.export.adapter.MPScanResult
import com.alipay.android.phone.scancode.export.adapter.MPScanStarter


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            TerminalTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Greeting(
                            name = "Android",
                            modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
        val scanRequest = ScanRequest()
        MPScan.startMPaasScanFullScreenActivity(
            this,
            scanRequest,
            object : MPScanCallbackAdapter() {
                override fun onScanFinish(
                    context: Context,
                    mpScanResult: MPScanResult,
                    mpScanStarter: MPScanStarter
                ): Boolean {
                    Toast.makeText(
                        applicationContext,
                        if (mpScanResult != null) mpScanResult.text else "没有识别到码",
                        Toast.LENGTH_SHORT
                    ).show()
                    (context as Activity).finish()
                    return true
                }
            })
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
            text = "Hello $name!",
            modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    TerminalTheme {
        Greeting("Android")
    }
}